var app = getApp();
var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    orderInfo: {},
    prepay_id: '',
    trade_sn: '',
    user_id: '',
    userCoupon: {},
    price: 0.00,
    money: 0.00,
    original_price: 0.00,
    price_offline: 0.00,
    checked: false
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数

    var that = this
    var orderInfo = wx.getStorageSync('orderInfo');

    this.setData({
      orderInfo: orderInfo,
      user_id: wx.getStorageSync('user_id'),
    })

    this.setData({
      price: orderInfo.price,
      money: orderInfo.money,
      original_price: orderInfo.original_price,
      price_offline: orderInfo.price_offline,
    });

    this.userCoupon();
  },

  //获取用户的优惠券
  userCoupon: function () {
    var that = this
    wx.request({
      url: api.userCoupon,
      data: {
        user_id: that.data.user_id,
      },
      header: {
        'content-type': 'application/json',
      },
      method: 'GET',
      success: function (res) {
        console.log(res);
        if (res.data.status === 1) {
          that.setData({
            userCoupon: res.data.result
          })
        }
      }
    })
  },

  listenCheckboxChange: function (e) {
    var that = this
    console.log(e);
    console.log(e.detail.value[0]/10);
    console.log(that.data.orderInfo)
    console.log(that.data.orderInfo.price);
    //选择优惠券
    if (e.detail.value) {
      if (e.detail.value[0] == 0.00) {
that.setData({
  money: 1.00, 
  price:1.00,
  price_offline:0.00
})
      } else {
      var couponPrice = that.data.orderInfo.original_price * (e.detail.value[0] / 10) * 0.1
      that.setData({
        money: couponPrice.toFixed(2),
        price: that.data.orderInfo.original_price * (e.detail.value[0] / 10),
        price_offline: that.data.orderInfo.original_price * (e.detail.value[0] / 10) - couponPrice.toFixed(2)
      });
      }
    }

    //取消勾选
    if (e.detail.value.length == 0) {
      that.setData({
        money: that.data.orderInfo.money,
        price: that.data.orderInfo.price,
        price_offline: that.data.orderInfo.price_offline,
      })
    }
  },
  onReady: function() {

  },
  onShow: function() {
    // 页面显示

  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },
  //向服务请求支付参数
  requestPayParam(trade_sn) {
    let that = this;

    util.request(api.WXPayPrepayId, {
      trade_sn: trade_sn,
      openid: app.globalData.token,
      price: that.data.money
    }, 'GET').then(function(res) {
      console.log('获取预支付参数'+res);
      if (res.state === 1) {
        wx.requestPayment({
          'timeStamp': res.timeStamp,
          'nonceStr': res.nonceStr,
          'package': res.package,
          'signType': res.signType,
          'paySign': res.paySign,
          'success': function(res) {
            wx.setStorageSync('trade_sn', trade_sn);
            //支付成功则进行模板消息的推送
            wx.request({
              url: api.sendTemplateMessage,
              data: {
                openid: app.globalData.token,
                trade_sn: trade_sn
              },
              method: 'GET',
              header: {
                "Content-type": "application/json",
              },
              success: function(res) {
                console.log(res);
              },
              fail: function(err) {
                console.log(err);
              },
              complete: function(res) {
                console.log(res);
              }
            })

            wx.redirectTo({
              url: '/pages/payResult/payResult?status=true',
            })
          },
          'fail': function(res) {
            wx.redirectTo({
              url: '/pages/payResult/payResult?status=false',
            })
          },
          complete:function(res) {
            console.log('支付完成回调'+res);
          }
        })
      }
    });
  },
  startPay(e) {
    this.requestPayParam(e.currentTarget.dataset.tradesn);
  }
})